/************************************************************************************/
/*  Program name CAPFNDIMP_CONC_PROG_FLEX.sql                                       */
/*  Creater:  WONG Wing Kin (PCCW - UniHub)                                         */
/*************   History   *************************                                */
/* 30 NOV, 2005   WONG Wing Kin         First created                               */
/************************************************************************************/
SET SERVEROUTPUT ON SIZE 1000000;

DECLARE
	lv_Prog_ID		VARCHAR2(100)	:=	'CAPFNDIMP_CONC_PROG_FLEX';

	lv_Scc_Count	NUMBER			:=	0;
	lv_Err_Count	NUMBER			:=	0;
	lv_Err_Msg		VARCHAR2(500)	:=	'No Error';

	CURSOR Cur_F IS
	SELECT
		*
	FROM
		CRST_FND_CONC_PROG_FLEX;

	lv_Next_Seq_ID	NUMBER;

BEGIN

	--  RENAME UNIQUE KEY ?
	UPDATE
		FND_DESCRIPTIVE_FLEXS
	SET
		Descriptive_Flexfield_Name	=	CAPBRIMP_MAPPING.RenameStr(SUBSTR(Descriptive_Flexfield_Name,7,4),Descriptive_Flexfield_Name)
	WHERE	(Application_ID,Descriptive_Flexfield_Name) IN
		(
			SELECT
				Application_ID,Descriptive_Flexfield_Name
			FROM
				CRST_FND_CONC_PROG_FLEX
		);

	FOR C1 IN Cur_F LOOP
		BEGIN
			INSERT INTO FND_DESCRIPTIVE_FLEXS
			VALUES
			(
			C1.APPLICATION_ID,
			C1.APPLICATION_TABLE_NAME,
			C1.DESCRIPTIVE_FLEXFIELD_NAME,
			C1.TABLE_APPLICATION_ID,
			C1.LAST_UPDATE_DATE,
			CAPBRIMP_COMMON.GetUser(C1.LAST_UPDATED_BY_NAME),		--LAST_UPDATE_BY,
			C1.CREATION_DATE,
			CAPBRIMP_COMMON.GetUser(C1.CREATED_BY_NAME),			--CREATED_BY,
			C1.LAST_UPDATE_LOGIN,
			C1.CONTEXT_REQUIRED_FLAG,
			C1.CONTEXT_COLUMN_NAME,
			C1.CONTEXT_USER_OVERRIDE_FLAG,
			C1.CONCATENATED_SEGMENT_DELIMITER,
			C1.FREEZE_FLEX_DEFINITION_FLAG,
			C1.PROTECTED_FLAG,
			C1.DEFAULT_CONTEXT_FIELD_NAME,
			C1.DEFAULT_CONTEXT_VALUE,
			C1.CONTEXT_DEFAULT_TYPE,
			C1.CONTEXT_DEFAULT_VALUE,
			C1.CONTEXT_OVERRIDE_VALUE_SET_ID,
			C1.CONTEXT_RUNTIME_PROPERTY_FUNCT
			);
		EXCEPTION
			WHEN OTHERS THEN
				DBMS_OUTPUT.Put_Line(SQLERRM);
				lv_Err_Count	:=	lv_Err_Count + 1;
		END;
		lv_Scc_Count	:=	lv_Scc_Count	+	1;
	END LOOP;

	IF	lv_Err_Count	>	0	THEN
		ROLLBACK;
	END IF;

	INSERT INTO CAPCNV_LOG(
		PROG_ID,
		PROCESS_DATE,
		RECORD_PROCESSED,
		RECORD_REJECTED,
		RECORD_EXTRACTED,
		ERR_MSG
	) VALUES (
		lv_Prog_ID,
		SYSDATE,
		lv_Scc_Count,
		lv_Err_Count,
		lv_Scc_Count-lv_Err_Count,
		lv_Err_Msg
	);

	COMMIT;
END;
/
